package com.tudouji.project.purchasebill.service.impl;

import java.util.List;

import com.tudouji.project.purchasebill.domain.Purchasebill;
import com.tudouji.project.purchasebill.mapper.PurchasebillMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.purchasebill.mapper.PurchasebillitemMapper;
import com.tudouji.project.purchasebill.domain.Purchasebillitem;
import com.tudouji.project.purchasebill.service.IPurchasebillitemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购单明细Service业务层处理
 * 
 * @author tudouji
 * @date 2022-04-11
 */
@Service
public class PurchasebillitemServiceImpl implements IPurchasebillitemService 
{
    @Autowired
    private PurchasebillitemMapper purchasebillitemMapper;
    @Autowired
    private PurchasebillMapper purchasebillMapper;

    /**
     * 查询采购单明细
     * 
     * @param guid 采购单明细ID
     * @return 采购单明细
     */
    @Override
    public Purchasebillitem selectPurchasebillitemById(String guid)
    {
        return purchasebillitemMapper.selectPurchasebillitemById(guid);
    }

    /**
     * 查询采购单明细列表
     * 
     * @param purchasebillitem 采购单明细
     * @return 采购单明细
     */
    @Override
    public List<Purchasebillitem> selectPurchasebillitemList(Purchasebillitem purchasebillitem)
    {
        return purchasebillitemMapper.selectPurchasebillitemList(purchasebillitem);
    }

    /**
     * 新增采购单明细
     * 
     * @param purchasebillitem 采购单明细
     * @return 结果
     */
    @Override
    public int insertPurchasebillitem(Purchasebillitem purchasebillitem)
    {
        return purchasebillitemMapper.insertPurchasebillitem(purchasebillitem);
    }

    /**
     * 修改采购单明细
     * 
     * @param purchasebillitem 采购单明细
     * @return 结果
     */
    @Override
    public int updatePurchasebillitem(Purchasebillitem purchasebillitem)
    {
        return purchasebillitemMapper.updatePurchasebillitem(purchasebillitem);
    }

    /**
     * 批量删除采购单明细
     * 
     * @param guids 需要删除的采购单明细ID
     * @return 结果
     */
    @Override
    public int deletePurchasebillitemByIds(String[] guids)
    {
        return purchasebillitemMapper.deletePurchasebillitemByIds(guids);
    }

    @Override
    @Transactional
    public int del(String guid)
    {
        Purchasebillitem purchasebillitem = purchasebillitemMapper.selectPurchasebillitemById(guid);
        Purchasebill purchasebill = purchasebillMapper.selectPurchasebillByGuid(purchasebillitem.getPurchaseguid());
        purchasebill.setTotalnum(purchasebill.getTotalnum().subtract(purchasebillitem.getCount()));
        purchasebill.setTotalamount(purchasebill.getTotalamount().subtract(purchasebillitem.getAmount()));
        purchasebillMapper.updatePurchasebill(purchasebill);
        return purchasebillitemMapper.deletePurchasebillitemById(guid);
    }

    /**
     * 删除采购单明细信息
     * 
     * @param guid 采购单明细ID
     * @return 结果
     */
    @Override
    public int deletePurchasebillitemById(String guid)
    {
        return purchasebillitemMapper.deletePurchasebillitemById(guid);
    }
}
